var ec2ui_LoadBalancerTreeView = {
    COLNAMES : ['loadbalancer.LoadBalancerName','loadbalancer.CreatedTime','loadbalancer.DNSName' ],
    treeBox : null,
    selection : null,
    ec2ui_session : null,
    retVal : null,
    arrLocalFiles : new Array(),
    LoadblancerList : new Array(),
    registered : false,

    get rowCount() { return this.LoadblancerList.length; },

    setTree      : function(treeBox)            { this.treeBox = treeBox; },
    getCellText : function(idx, column) {
        if (idx >= this.rowCount) return "";
        var member = column.id.split(".").pop();
        return this.LoadblancerList[idx][member];
    },
    isEditable: function(idx, column)  { return true; },
    isContainer: function(idx)            { return false;},
    isSeparator: function(idx)            { return false; },
    isSorted: function()                    { return false; },

    getImageSrc: function(idx, column) { return ""; },
    getProgressMode : function(idx,column) {},
    getCellValue: function(idx, column) {},
    cycleHeader: function(col) {
        var loadbalancer = this.getSelectedLoadbalancer();
        cycleHeader(
        col,
        document,
        this.COLNAMES,
        this.LoadblancerList);
        this.treeBox.invalidate();
        if (loadbalancer) {
            log(loadbalancer.LoadBalancerName + ": Select this Monitor Instance post sort");
            this.selectByName(LoadBalancer.LoadBalancerName);
        } else {
            log("The selected Monitor Instance is null!");
        }

    },
    sort : function() {
     var LoadBalancer = this.getSelectedLoadbalancer();
        sortView(document, this.COLNAMES, this.LoadblancerList);
        if (LoadBalancer) this.selectByName(LoadBalancer.LoadBalancerName);
    },
    
    selectionChanged: function() {},
    cycleCell: function(idx, column) {},
    performAction: function(action) {},
    performActionOnCell: function(action, index, column) {},
    getRowProperties: function(idx, column, prop) {},
    getCellProperties: function(idx, column, prop) {},
    getColumnProperties: function(column, element, prop) {},
    getLevel : function(idx) { return 0; },
    
    getSelectedLoadbalancer : function() {
      
        var index =  this.selection.currentIndex;
       
        if (index == -1) return null;
       
        return this.LoadblancerList[index];
        
    },
     selectByName : function(Value) {
        this.selection.clearSelection();
        for(var i in this.LoadblancerList) {
             if (this.LoadblancerList[i].LoadBalancerName == LoadBalancerName) {
                this.selection.select(i);
                this.treeBox.ensureRowIsVisible(i);
                return;
            }
        }

        // In case we don't find a match (which is probably a bug).
        this.selection.select(0);
    },
    
    register: function() {
        if (!this.registered) {
            this.registered = true;
            ec2ui_model.registerInterest(this, 'loadbalancer');
        }
    },

    invalidate: function() {
        this.displayLoadBalancer(ec2ui_session.model.loadbalancer);
    },

    refresh: function() {
        ec2ui_session.controller.describeLoadBalancers();
    },
    
    // Create Load Balancer
   createlb: function() {
        var retVal = {ok:null};
        window.openDialog(
            "chrome://ec2ui/content/dialog_create_lb.xul",
            null,
            "chrome,centerscreen,modal",
            ec2ui_session,
            retVal,
            null
           );
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
    if (retVal.ok) {        
   ec2ui_session.controller.createlb(retVal.LoadBalancerName,retVal.Protocol,retVal.elbport,retVal.instanceport,wrap);
   ec2ui_session.controller.configurehealthchk(retVal.LoadBalancerName,retVal.Interval,retVal.Timeout,retVal.HealthyThreshold,retVal.UnhealthyThreshold,wrap);
    }
     },
     
    //configure Health Check
     configurehealth: function() {
      var loadbalancer = this.getSelectedLoadbalancer();
      if (loadbalancer == null) return;
      var retVal = {ok:null};
      window.openDialog(
            "chrome://ec2ui/content/test_dialog.xul",
            null,
            "chrome,centerscreen,modal",
            ec2ui_session,
            retVal,
            null
           );
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
    if (retVal.ok) {        
   //ec2ui_session.controller.createlb(retVal.LoadBalancerName,retVal.Protocol,retVal.elbport,retVal.instanceport,wrap);
   ec2ui_session.controller.configurehealthchk(loadbalancer.LoadBalancerName,retVal.Interval,retVal.Timeout,retVal.HealthyThreshold,retVal.UnhealthyThreshold,wrap);
      }
     },
     
    //Register instance with loadbalancer 
    registerinstances : function(){
        var retVal = {ok:null};
         window.openDialog(
            "chrome://ec2ui/content/dialog_register_instancelb.xul",
            null,
            "chrome,centerscreen,modal",
            null,
            ec2ui_session,
            retVal
            );
   
    //ec2ui_session.controller.registerlbinstance(retVal.Instances);
    //return retVal.ok;
    },
    //view More Details
    viewDetails : function(){
      var loadbalancer = this.getSelectedLoadbalancer();
      if (loadbalancer == null) return;
       window.openDialog(
            "chrome://ec2ui/content/dialog_loadbalancer_details.xul",
            null,
            "chrome,centerscreen,modal",
            loadbalancer);
        
    },
    // Delete Load balancer
    deletelb : function(){
    var loadbalancer = this.getSelectedLoadbalancer();
      if (loadbalancer == null) return;
        var confirmed = confirm("Delete Loadbalancer "+loadbalancer.LoadBalancerName+"?");
        if (!confirmed)
            return;
        var me = this;
        var wrap = function() {
            if (ec2ui_prefs.isRefreshOnChangeEnabled()) {
                me.refresh();
            }
        }
        ec2ui_session.controller.deleteloadbalancer(loadbalancer.LoadBalancerName, wrap);     
 
    },
    //copy to clip board
    copyToClipBoard : function(fieldName) {
        var loadbalancer = this.getSelectedLoadbalancer();
        if (loadbalancer == null) {
            return;
        }

        copyToClipboard(loadbalancer[fieldName]);
    },

    notifyModelChanged: function(interest) {
        this.invalidate();
    },

    displayLoadBalancer : function (LoadblancerList) {
        if (!LoadblancerList) { LoadblancerList = []; }

        this.treeBox.rowCountChanged(0, -this.LoadblancerList.length);
        this.LoadblancerList = LoadblancerList;
        this.treeBox.rowCountChanged(0, this.LoadblancerList.length);
        this.sort();
        this.selection.clearSelection();
        if (LoadblancerList.length > 0) {
            this.selection.select(0);
        }
    }
};

ec2ui_LoadBalancerTreeView.register();
